package com.alibaba.p3c.pmd.lang.java.rule.oop;

import com.alibaba.p3c.pmd.lang.java.rule.AbstractAliRule;
import jdk.nashorn.internal.ir.BlockStatement;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.*;

import java.util.List;

/**
 * @author 郭恒坤
 * @date 2020/4/14
 * @description 不能使用System.out.print
 */

public class CanNotUseSystemOutPrintRule extends AbstractAliRule {

    private static final String SYSTEM_OUT = "System.out";

    @Override
    public Object visit(ASTClassOrInterfaceBodyDeclaration node, Object data) {
        try {
            List<ASTPrimaryPrefix> astPrimaryPrefixes = node.findDescendantsOfType(ASTPrimaryPrefix.class);
            if (astPrimaryPrefixes != null && astPrimaryPrefixes.size() > 0) {
                for (ASTPrimaryPrefix astPrimaryPrefix : astPrimaryPrefixes) {
                    List<ASTName> astNames = astPrimaryPrefix.findChildrenOfType(ASTName.class);
                    if (astNames != null && astNames.size() > 0){
                        for (ASTName astName : astNames){
                            if (astName.getImage().contains(SYSTEM_OUT)){
                                addViolationWithMessage(data, astPrimaryPrefix, "java.oop.CanNotUseSystemOutPrintRule.violation.msg", new Object[]{astPrimaryPrefix.getImage()});
                            }
                        }
                    }
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return super.visit(node, data);
    }
}
